<?php
/**
 * Created by PhpStorm.
 * User: ly
 * Date: 2017\12\25 0025
 * Time: 10:26
 */

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\bootstrap\Alert;

$this->registerCssFile('/css/officeHall/uploadAssetPackage/initiateSynergy.css');
$this->registerCssFile('/css/adminLTE/select2.min.css',['depends'=>'\yii\bootstrap\BootstrapAsset']);
$this->registerCssFile('/css/adminLTE/font-awesome.min.css',['depends'=>'\yii\bootstrap\BootstrapAsset']);


$this->registerJsFile('/js/adminLTE/select2.full.min.js');
$this->registerJsFile('/js/adminLTE/moment.min.js');


if(!isset($no_ajax)) $no_ajax=false;
if( Yii::$app->getSession()->hasFlash('success') ) {
    echo Alert::widget([
        'options' => [
            'class' => 'alert-success', //这里是提示框的class
        ],
        'body' => Yii::$app->getSession()->getFlash('success'), //消息体
    ]);
}
if( Yii::$app->getSession()->hasFlash('error') ) {
    echo Alert::widget([
        'options' => [
            'class' => 'alert-error',
        ],
        'body' => Yii::$app->getSession()->getFlash('error'),
    ]);
}
if(isset($projectid)) {
    ?>
    <!-- Modal -->
    <div class="modal-dialog" role="document" <?php if($no_ajax)echo 'style="margin: 5px 5px;"';?> >
        <?php $form = ActiveForm::begin([
            'action' => ['project/set-team'], //提交地址(*可省略*)
            'method' => 'post',    //提交方法(*可省略默认POST*)
            'id' => 'set-team', //设置ID属性
            'options' => [
                'class' => 'myForm form-inline', //设置class属性
            ]
        ]); ?>
        <div class="modal-content">
            <div class="modal-header">
                <?php
                if(!$no_ajax) {
                    ?>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true"><img src="/image/assetPackage/close.png"></span></button>
                    <?php
                }
                    ?>
                <div class="pull-left modal-nav">
                    <img src="/image/assetPackage/white.png" class="pull-left">
                    <span class="pull-left">配置团队</span>
                </div>
            </div>
            <div class="modal-body">
                <div class="modal-body-div">
                    <input type="hidden" name="projectid" id="projectid" value="<?php echo $projectid; ?>"/>
                    <input type="hidden" name="no_ajax" id="no_ajax" value="<?php echo ($no_ajax)?$no_ajax:0; ?>"/>

                        <div class="clearfix" style="padding:10px 0;">
                            <div class="col-xs-3">
                            <label for="" class="pull-left">项目总监</label>
                            </div>
                            <div class="col-xs-6">
                            <select class="form-control select212" data-placeholder="Select a State"
                                    style="width: 100%;">
                                <option value="0">请选择</option>
                                <?php
                                if (!empty($userChief)){
                                    foreach ($userChief as $k => $v) {
                                        $username = $v['realname'];
                                        $id       = $v['id'];
                                        echo "<option value='".$id."'>".$username."</option>";
                                    }
                                }
                                ?>
                            </select>
                            </div>
                        </div>
                        <div class="clearfix"  style="padding:10px 0;">
                            <div class="col-xs-3">
                            <label for="" class="pull-left">项目经理</label>
                            </div>
                            <div class="col-xs-6">
                            <select class="form-control select3" data-placeholder="Select a State"
                                    style="width: 100%;">
                                <option value="0">请选择</option>
                                <?php
                                if (!empty($user)&& is_array($user)){
                                    foreach ($user as $k => $v) {
                                        $username = $v['realname'];
                                        $id       = $v['id'];
                                        echo "<option value='".$id."'>".$username."</option>";
                                    }
                                }
                                ?>
                            </select>
                            </div>
                        </div>
                        <div class="clearfix"  style="padding:10px 0;">
                            <div class="col-xs-3">
                            <label for="" class="pull-left">团队成员</label>
                            </div>
                            <div class="col-xs-6">
                            <select class="form-control select4" multiple="multiple" data-placeholder="Select a State"
                                    style="width: 100%;">
                                <?php
                                if (!empty($user) && is_array($user)){
                                    foreach ($user as $k => $v) {
                                        $user = $v['realname'];
                                        $id       = $v['id'];
                                        echo "<option value='".$id."'>".$user."</option>";
                                    }
                                }
                                ?>
                            </select>
                            </div>
                        </div>
                        <div class="clearfix"  style="padding:10px 0;">
                            <div class="col-xs-3">
                            <label for="" class="pull-left">其他说明</label>
                            </div>
                            <br />
                            <div class="col-xs-6">
                            <textarea name="" style="width: 100%;" class="form-control textarea-edit" id="content" ><?= !empty($team['explain'])?$team['explain']:''?></textarea>
                            </div>
                        </div>
                </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default btns-primary" data-dismiss="modal">取消</button>
                    <button type="button" id="set-team-btn" class="btn btn-primary btns-primary">提交</button>
                </div>
        </div>
        <?php ActiveForm::end() ?>
    </div>
    <?php
}
    ?>

    <!-- textarea 高度设置对比div -->
<div style="max-height: 1px; overflow: hidden;">
    <div id="d1" style="visibility:hidden" class="one-txt-cut"></div>
</div>

<script type="text/javascript">
    $(function(){
         // textarea 高度展示问题
        $(document).on("keyup",".textarea-edit",function(){
            console.log($(this).width());
            var w = $(this).width();
            $("#d1").width(w);
            var val = $(this).val();
            $("#d1").html("<pre>" +val+" "+"</pre>");
            $("#d1").find("pre").addClass("one-txt-cut");
            var h = $("#d1").height() ;
            $(this).height(h);
            console.log(h);
        })
    });
</script>

    <script>
        $(function () {
            //初始化值
            project_id = $("#projectid").val();
            var chiefStr = JSON.parse('<?= json_encode($teamZJ)?>');
            var chiefId = $.grep( '<?=  $team['manager'] ?>'.split(','), function(n) {return $.trim(n).length > 0;});
            var leaderStr =  JSON.parse('<?= json_encode($teamFZR)?>');
            var leaderId =  $.grep( '<?=  $team['owner'] ?>'.split(','), function(n) {return $.trim(n).length > 0;});
            var userStr = JSON.parse('<?= json_encode($teamXZR)?>');
            var userId = $.grep( '<?=  $team['partner'] ?>'.split(','), function(n) {return $.trim(n).length > 0;});
            //Initialize Select2 Elements
            $('.select212').select2({
                data: chiefStr,
            });
            $('.select212').select2('val',chiefId);
            $('.select3').select2({
                data: leaderStr,
            });
            $('.select3').select2('val',leaderId);
            $('.select4').select2({
                data: userStr,
            }).val(userId).trigger('change');
            $("#set-team-btn").click(function () {


                var zongjian = $('.select212').select2('data');
                var zongjianArr = [];
                $.each(zongjian,function(i,j){
                    //i为元素的索引，从0开始,j为当前处理的元素对象
                    zongjianArr.push(j.text);
                });
                var jingli = $('.select3').select2('data');
                var jingliArr = [];
                $.each(jingli,function(i,j){
                    //i为元素的索引，从0开始,j为当前处理的元素对象
                    jingliArr.push(j.text);
                });
                var chenyuan = $('.select4').select2('data');
                var chenyuanArr = [];
                $.each(chenyuan,function(i,j){
                    //i为元素的索引，从0开始,j为当前处理的元素对象
                    chenyuanArr.push(j.text);
                });

                var dataChief = zongjianArr;
                var dataLeader= jingliArr;
                var dataUser  = chenyuanArr;
                var content = $("#content").val();
                $.post('/project/ajax-save-team-project',{'dataChief':dataChief,'dataLeader':dataLeader,'dataUser':dataUser,'content':content,'project_id':project_id},function (res) {
                    if (res.code==200){
                        layer.msg("更新成功:"+res.message);
                        window.location.reload();
                    }else{
                        layer.msg("更新失败"+res.message);
                    }
                }, "json");
            })


            $.fn.modal.Constructor.prototype.enforceFocus = function () { };//让select2选择框可以输入

        })
    </script>
<?php

$css = <<<css

/*访问权限*/

 .jurisdiction input[type="radio"] {
	display: none;
	height: 0;
	width: 0;
}

 .jurisdiction label {
	margin-left: 48px;
	height: 24px;
	position: relative;
	text-indent: 6px;
	cursor: pointer;
	position: relative;
	margin-right: 36px;
}

 .jurisdiction label span {
	display: inline-block;
	height: 24px;
	width: 24px;
	background: url(../../../image/officeHall/uploadAssetPackage/circular1.png) no-repeat center;
	z-index: 2;
	position: absolute;
	left: -22px;
	font-size: 14px;
	color: #adadad;
	font-weight: bold;
}

 .jurisdiction .radioChecked {
	color: #000000;
}

 .jurisdiction .radioChecked span {
	position: absolute;
	left: -32px;
	font-size: 14px;
	margin-right: 17px;
	color: #adadad;
	font-weight: bold;
	margin-right: 57px;
	margin-left: 10px;
	background: url(../../../image/officeHall/uploadAssetPackage/circular2.png) no-repeat left 1px;
	padding-left: 25px;
}


/*//公开部门*/

 .PublicSector>input {
	margin-left: 92px;
}

 .PublicSector #oneSector {
	margin-left: 10px;
}

 .PublicSector .sectorList {
	/*display: none;*/
	width: auto;
	height: auto;
	border: 1px solid #CCCCCC;
	border-radius: 5px;
	margin-left: 50px;
	margin-right: 60px;
	padding: 10px;
}

 .PublicSector .sectorList label {
	margin-right: 10px;
	width: auto;
	font-size: 13px;
}

 .nextStep {
	width: 100%;
	height: 30px;
	margin-top: 40px;
	position: relative;
	margin-bottom: 40px;
}

 .nextStep .btn-primary {
	position: absolute;
	right: 30px;
	bottom: 30px;
}
css;

$this->registerCss($css);

$js = <<<JS


//此处点击按钮提交数据的jquery
$('#set-permission-btn').click(function () {
  if($("#no_ajax").val()==1){
    $("form").submit();  
  }else{
   $.ajax({
        url:  '/project/set-permissions',
        type: "POST",
        dataType: "json",
        data: $('form').serialize(),
        success: function(Data) {
            if(Data.status){
                layer.msg(Data.msg);
                $('#myModal').modal('hide');
            }else{
                 layer.msg(Data.msg);
            }
        },
        error: function() {
             layer.msg("网络错误！");
        }
    });   
  }

    return false;
});

JS;
//$this->registerJs($js);
